package com.example.dormmanage.controller;

import com.example.dormmanage.entity.Admin;
import com.example.dormmanage.entity.Student;
import com.example.dormmanage.service.impl.AdminService;
import com.example.dormmanage.service.impl.StudentService;
import com.example.dormmanage.service.impl.UserService;
import com.example.dormmanage.view.ResultView;
import com.example.dormmanage.view.UserVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
@Slf4j
@Api(tags = "用户登录退出接口")
public class UserController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private AdminService adminService;

    @Autowired
    private UserService userService;

    @ApiOperation("学生登录")
    @PostMapping("/login/student")
    public ResultView userLogin(@RequestBody Student student){
        String userName = student.getUserName()+"@student";
        student.setUserName(userName);
        return studentService.studentLogin(student);
    }

    @ApiOperation("管理员登录")
    @PostMapping("/login/admin")
    public ResultView userLogin(@RequestBody Admin admin){
        String userName = admin.getUserName()+"@admin";
        admin.setUserName(userName);
        return adminService.adminLogin(admin);
    }

    @ApiOperation("学生退出登录")
    @PostMapping("/loginOut/student")
    public ResultView<String> LoginOutByStudent(){
        log.info("学生退出登录");
        return studentService.loginOutByStudent();
    }

    @ApiOperation("管理员退出登录")
    @PostMapping("/loginOut/admin")
    public ResultView<String> LoginOutByAdmin(){
        return adminService.loginOutByAdmin();
    }

    @Transactional(rollbackFor = Exception.class)//开启事务
    @ApiOperation("用户修改密码")
    @PostMapping("/login/updatePassword")
    public ResultView<String> updatePassword(@RequestBody UserVo vo){
        System.out.println(vo.getPassword());
        return userService.updatePassword(vo);
    }

    @ApiOperation("用户功能测试接口")
    @GetMapping("/hello")
    @PreAuthorize("hasAuthority('test')")
    public String hello(){
        return "hello";
    }
}
